In [1]:
from IPython import parallel
clients = parallel.Client(profile='parallel')
In [2]:
print clients.ids
print "Total %i cores"%(len(clients.ids))
In [3]:
%%px --local
import cPickle as pickle
import glob
import os
In [4]:
%%px --local
import sys
sys.path.append("\\\\DAP-NAS\\work\\CNOP")
import cPickle as pickle
import numpy as np
import pandas as pd
from statsmodels.tsa.arima_process import arma_generate_sample
import matplotlib.pyplot as plt
import numpy as np
from CNOP import CNOP
import winsound
def threshold(x,thresholds=[],values=[-1,0,1]):
for threshold,val in zip(thresholds,values):
if x < threshold:
return val
return values[-1]
import time
from itertools import repeat
import os
from datetime import datetime
import numpy as np
import numpy.linalg as linalg
In [5]:
%%px --local
def pickle_folder(path, wait_interactive=True, chunksize=1):
'''Pickles all objects (except !README.txt) from the folder assynchronically'''
try:
print "INFO FROM FILE:", file(path+"!README.txt").read()
except:
print "NO README file"
view = clients.load_balanced_view()
files = os.listdir(path)
if len(files) < 2:
raise RuntimeError, "Less than 2 files"
ar = view.map_async(lambda x: pickle.load(file(x)),
[path+x for x in os.listdir(path) if x != "!README.txt"],
chunksize=chunksize)
if wait_interactive:
ar.wait_interactive()
return ar.get()
else:
return ar
def get_folder_info(path):
print "INFO FROM FILE:", file(path+"!README.txt").read()
files = os.listdir(path)
print "With %i files"%(len(files))
In [44]:
get_folder_info("\\\\DAP-NAS\\work\\CNOP\\dumps\\temp\\20150323_212714\\")
In [45]:
res250Full=pickle_folder("\\\\DAP-NAS\\work\\CNOP\\dumps\\temp\\20150323_212714\\")
In [46]:
len(res250Full)
Out[46]:
In [48]:
pickle.dump(res250Full, file("\\\\DAP-NAS\\work\\CNOP\\dumps\\MC 31.03-results\\3Full\\res250_CHECKED", "w"))
In [54]:
del res250Full
In [49]:
get_folder_info("\\\\DAP-NAS\\work\\CNOP\\dumps\\temp\\20150324_101209\\")
In [55]:
res500Full=pickle_folder("\\\\DAP-NAS\\work\\CNOP\\dumps\\temp\\20150324_101209\\")
In [56]:
len(res500Full)
Out[56]:
In [57]:
pickle.dump(res500Full, file("\\\\DAP-NAS\\work\\CNOP\\dumps\\MC 31.03-results\\3Full\\res500_CHECKED", "w"))
In [59]:
get_folder_info("\\\\DAP-NAS\\work\\CNOP\\dumps\\temp\\20150325_051321\\")
In [60]:
res1000Full0=pickle_folder("\\\\DAP-NAS\\work\\CNOP\\dumps\\temp\\20150325_051321\\")
In [62]:
len(res1000Full0)
Out[62]:
In [63]:
res1000Full1=pickle_folder("\\\\DAP-NAS\\work\\CNOP\\dumps\\temp\\20150326_120353\\")
In [64]:
pickle.dump(res1000Full0+res1000Full1, file("\\\\DAP-NAS\\work\\CNOP\\dumps\\MC 31.03-results\\3Full\\res1000_CHECKED", "w"))
In [66]:
get_folder_info("\\\\DAP-NAS\\work\\CNOP\\dumps\\temp\\20150323_212714\\")
In [24]:
get_folder_info("\\\\DAP-NAS\\work\\CNOP\\dumps\\temp\\20150328_094006\\")
In [25]:
get_folder_info("\\\\DAP-NAS\\work\\CNOP\\dumps\\temp\\20150328_004236\\")
In [26]:
res500No0=pickle_folder("\\\\DAP-NAS\\work\\CNOP\\dumps\\temp\\20150328_094006\\")
In [27]:
res500No1=pickle_folder("\\\\DAP-NAS\\work\\CNOP\\dumps\\temp\\20150328_004236\\")
In [34]:
res500No0 = [[i, ""] for i in res500No0]
res500No1 = [[i, ""] for i in res500No1]
In [37]:
pickle.dump(res500No0+res500No1, file("\\\\DAP-NAS\\work\\CNOP\\dumps\\MC 31.03-results\\1No+\\res500_CHECKED", "w"))
In [70]:
del res100Full
In [81]:
res1000_NOT_CHECKED=pickle.load(file("\\\\DAP-NAS\\work\\CNOP\\dumps\\MC 31.03-results\\2Partial\\res1000_NOT_CHECKED"))
In [82]:
len(res1000_NOT_CHECKED)
Out[82]:
In [2]:
import numpy as np
import numpy.linalg as linalg
In [6]:
def process_dump(path, res_real):
mc_res = pickle.load(file(path))
xs = np.array([item[0][0].x for item in mc_res if len(item)==2 and linalg.norm(item[0][0].x, ord=np.inf) < 100 ])
ses = np.array([item[0][0].se for item in mc_res
if len(item)==2 and linalg.norm(item[0][0].x, ord=np.inf) < 100 \
and "se" in item[0][0].keys() and not np.isnan(item[0][0].se).any() \
#and linalg.norm(item[0][0].se, ord=np.inf) < 100 ])
])
xs = pd.Dataframe(xs)
ses = pd.Dataframe(ses)
rmse = ((res_real - xs) ** 2).mean()
bias = (res_real - xs).mean()
a_ratio = (ses.mean()/xs.std()).mean()
m_ratio = (ses.median()/xs.std()).mean()
print "FILE: %s"%(path.split("\\")[-1])
print "BIAS: %2.3f"%(bias)
print "RMSE: %2.3f"%(rmse)
print "A-ratio: %s"%(a_ratio)
print "M-ratio: %s"%(m_ratio)
print
print "XS len: %s" % len(xs)
print "SE len: %s" % len(ses)
print "XS variance: %s "% xs.std(axis=0)
In [12]:
res1000_NOT_CHECKED = pickle.load(file(u"C:\\Users\\User\\Dropbox\\Лекции\\ВКР\\My Monte-carlo\\MC 31.03-results\\2Partial\\res100_CHECKED"))
In [116]:
path=u"C:\\Users\\User\\Dropbox\\Лекции\\ВКР\\My Monte-carlo\\MC 31.03-results\\2Partial\\res100_CHECKED"
process_dump(path, res_real)
In [7]:
beta, alpha = [0.6, 0.4, 0.8], [0.85, 1.55]
gammam, mum = [0.4, 0.3, 0.9], [-1.2, 0.07]
gammap, mup = [0.2, 0.8, 0.3], [1.28, 2.5]
res_real_full = beta+alpha+gammam+mum+gammap+mup
beta, alpha = [0.6, 0.4], [0.9, 1.5]
gammam, mum = [0.3, 0.9], [-0.67, 0.36]
gammap, mup = [0.2, 0.3], [0.02, 1.28]
res_real_partial = beta+alpha+gammam+mum+gammap+mup
beta, alpha = [0.6], [0.95, 1.45]
gammam, mum = [0.9], [-1.22, 0.03]
gammap, mup = [0.8], [-0.03, 1.18]
res_real_no = beta+alpha+gammam+mum+gammap+mup
In [8]:
beta, alpha = [0.6, 0.4], [0.9, 1.5]
gammam, mum = [0.3, 0.9], [-0.67, 0.36]
gammap, mup = [0.2, 0.3], [0.02, 1.28]
res_real_partial = beta+alpha+gammam+mum+gammap+mup
In [9]:
beta, alpha = [0.6], [0.95, 1.45]
gammam, mum = [0.9], [-1.22, 0.03]
gammap, mup = [0.8], [-0.03, 1.18]
res_real_no = beta+alpha+gammam+mum+gammap+mup
In [191]:
path=u"W:\\CNOP\\dumps\\MC 31.03-results\\3Full\\"
process_dump(path + "res150_CHECKED", res_real_full)
In [198]:
path=u"W:\\CNOP\\dumps\\MC 31.03-results\\3Full\\"
process_dump(path + "res150_CHECKED", res_real_full)
In [200]:
process_dump("W:\\CNOP\\dumps\\MC 31.03-results\\2Partial\\res150_CHECKED", res_real_partial)
In [201]:
process_dump("W:\\CNOP\\dumps\\MC 31.03-results\\1No+\\res150_CHECKED", res_real_no)
In [ ]:
In [17]:
path_no=u"W:\\CNOP\\dumps\\MC 31.03-results\\1No+\\"
process_dump(path_no + "res100_CHECKED", res_real_no)
print
process_dump(path_no + "res250_CHECKED", res_real_no)
print
#process_dump(path_no + "res500_CHECKED", res_real_no)
print
process_dump(path_no + "res1000_OK", res_real_no)
In [174]:
path_no=u"W:\\CNOP\\dumps\\MC 31.03-results\\1No+\\"
process_dump2(path_no + "res500_WRONG_FORMAL", res_real_no)
In [162]:
mc_res = pickle.load(file(path_no+"res500_WRONG_FORMAL"))
res_real = res_real_no
In [168]:
mc_res[210][0].x
Out[168]:
In [ ]:
for i in
In [172]:
xs = np.array([item[0].x for item in mc_res if type(item) != IndexError and type(item[0])!=int
and linalg.norm(item[0].x, ord=np.inf) < 100 \
and len(item[0].x) == len(res_real)])
ses = np.array([item[0].se for item in mc_res
if type(item) != IndexError and type(item[0])!=int \
and linalg.norm(item[0].x, ord=np.inf) < 100 \
and "se" in item[0].keys() and not np.isnan(item[0].se).any()\
and len(item[0].x) == len(res_real)])
rmse = ((res_real - xs) ** 2).mean()
bias = (res_real - xs).mean()
a_ratio = (ses.mean(axis=0)/xs.std(axis=0)).mean()
m_ratio = (np.median(ses, axis=0)/xs.std(axis=0)).mean()
print "FILE: %s"%(path.split("\\")[-1])
print "BIAS: %2.3f"%(bias)
print "RMSE: %2.3f"%(rmse)
print "A-ratio: %s"%(a_ratio)
print "M-ratio: %s"%(m_ratio)
In [175]:
len(xs)
Out[175]:
In [ ]:
In [13]:
mc_res = pickle.load(file(path_no+"res500_CHECKED"))
xs = np.array([item[0][0].x for item in mc_res if len(item)==2 and linalg.norm(item[0][0].x, ord=np.inf) < 100 ])
In [15]:
xs=[]
for i,item in enumerate(mc_res):
print i
if len(item)==2 and linalg.norm(item[0][0].x, ord=np.inf) < 100 :
xs.append(item[0][0].x)
In [38]:
process_dump(path_no + "res500_CHECKED", res_real_no)
In [30]:
mc_res = pickle.load(file(path_no+"res100_CHECKED"))
In [56]:
res500No0[1][0][0].x
Out[56]:
In [57]:
mc_res[2][0][0].x
Out[57]:
In [58]:
path_no=u"W:\\CNOP\\dumps\\MC 31.03-results\\2Partial\\"
process_dump(path_no + "res100_CHECKED", res_real_partial)
print
process_dump(path_no + "res250_CHECKED", res_real_partial)
print
process_dump(path_no + "res500_CHECKED", res_real_partial)
print
process_dump(path_no + "res1000_CHECKED", res_real_partial)
In [156]:
path_no=u"W:\\CNOP\\dumps\\MC 31.03-results\\3Full\\"
process_dump2(path_no + "res100_CHECKED", res_real_full)
print
process_dump2(path_no + "res250_CHECKED", res_real_full)
print
process_dump2(path_no + "res500_CHECKED", res_real_full)
print
process_dump2(path_no + "res1000_CHECKED", res_real_full)
In [184]:
path_no=u"W:\\CNOP\\dumps\\MC 31.03-results\\3Full\\"
process_dump2(path_no + "res1000_CHECKED", res_real_full)
In [179]:
xs = np.array([item[0].x for item in mc_res if type(item) != IndexError and type(item[0])!=int
and type(item[0])!=tuple
and linalg.norm(item[0].x, ord=np.inf) < 100 \
and len(item[0].x) == len(res_real)])
ses = np.array([item[0].se for item in mc_res
if type(item) != IndexError and type(item[0])!=int \
and type(item[0])!=tuple
and linalg.norm(item[0].x, ord=np.inf) < 100 \
and "se" in item[0].keys() and not np.isnan(item[0].se).any()\
and len(item[0].x) == len(res_real)])
rmse = ((res_real - xs) ** 2).mean()
bias = (res_real - xs).mean()
a_ratio = (ses.mean(axis=0)/xs.std(axis=0)).mean()
m_ratio = (np.median(ses, axis=0)/xs.std(axis=0)).mean()
print "FILE: %s"%(path.split("\\")[-1])
print "BIAS: %2.3f"%(bias)
print "RMSE: %2.3f"%(rmse)
print "A-ratio: %s"%(a_ratio)
print "M-ratio: %s"%(m_ratio)
In [183]:
del mc_res
In [ ]:
In [ ]:
In [8]:
def process_dump2(path, res_real):
mc_res = pickle.load(file(path))
xs = np.array([item[0].x for item in mc_res if type(item) != IndexError and type(item[0])!=int
and type(item[0])!=tuple
and linalg.norm(item[0].x, ord=np.inf) < 100 \
and len(item[0].x) == len(res_real)])
ses = np.array([item[0].se for item in mc_res
if type(item) != IndexError and type(item[0])!=int \
and type(item[0])!=tuple
and linalg.norm(item[0].x, ord=np.inf) < 100 \
and "se" in item[0].keys() and not np.isnan(item[0].se).any()\
and len(item[0].x) == len(res_real)])
rmse = ((res_real - xs) ** 2).mean()
bias = (res_real - xs).mean()
a_ratio = (ses.mean(axis=0)/xs.std(axis=0)).mean()
m_ratio = (np.median(ses, axis=0)/xs.std(axis=0)).mean()
print "FILE: %s"%(path.split("\\")[-1])
print "BIAS: %2.3f"%(bias)
print "RMSE: %2.3f"%(rmse)
print "A-ratio: %s"%(a_ratio)
print "M-ratio: %s"%(m_ratio)
In [98]:
mc_res = pickle.load(file(path_no + "res100_CHECKED"))
xs = np.array([item[0].x for item in mc_res if type(item) != IndexError and linalg.norm(item[0].x, ord=np.inf) < 100 ])
ses = np.array([item[0].se for item in mc_res
if type(item) != IndexError and linalg.norm(item[0].x, ord=np.inf) < 100 \
and "se" in item[0].keys() and not np.isnan(item[0].se).any()])
In [ ]:
xs=[]
for i, item in enumerate(mc_res):
print i
if type(item) != IndexError and linalg.norm(item[0].x, ord=np.inf) < 100:
xs.append(item[0].x)
In [128]:
xs = [item[0].x for item in mc_res if type(item) != IndexError and linalg.norm(item[0].x, ord=np.inf) < 100 and len(item[0].x) = len(res_resl)]
In [ ]:
np.asarray(xs[0:131])
In [147]:
len(xs[130])
Out[147]:
In [126]:
arrs = [np.array([1,2,3]), np.array([4,5,6]), np.array([7,8,9])]
In [127]:
arrs
Out[127]:
In [9]:
def process_dump(obj, res_real, cutpoints = (.2, .8)):
if type(obj) is str:
mc_res = pickle.load(file(obj))
else:
mc_res = obj
xs = np.array([item[0].x for item in mc_res if type(item)==tuple and item[0].success
])
ses = np.array([np.array(item[0].se)[0] for item in mc_res
if type(item)==tuple and item[0].success
#and linalg.norm(item[0][0].x, ord=np.inf) < 100
and "se" in item[0].keys() #and not np.isnan(item[0][0].se).any() \
#and linalg.norm(item[0][0].se, ord=np.inf) < 100
])
ses = np.nan_to_num(ses)
xs = pd.DataFrame(xs)
ses = pd.DataFrame(ses)
rmse = ((res_real - xs) ** 2).mean().mean()
bias = (res_real - xs).mean().mean()
#a_ratio = (ses.mean()/xs.std()).mean()
#m_ratio = (ses.median()/xs.std()).mean()
a_ratio = (ses[(ses<ses.quantile(cutpoints[1]))&(ses>ses.quantile(cutpoints[0]))].mean() \
/ xs[(xs<xs.quantile(cutpoints[1]))&(xs>xs.quantile(cutpoints[0]))].std()).mean()
m_ratio = (ses[(ses<ses.quantile(cutpoints[1]))&(ses>ses.quantile(cutpoints[0]))].median() \
/ xs[(xs<xs.quantile(cutpoints[1]))&(xs>xs.quantile(cutpoints[0]))].std()).mean()
if type(obj) is str:
print "FILE: %s"%(obj.split("\\")[-1])
print "BIAS: %2.3f"%(bias)
print "RMSE: %2.3f"%(rmse)
print "A-ratio: %2.3f"%(a_ratio)
print "M-ratio: %2.3f"%(m_ratio)
print
print "XS len: %s" % len(xs)
print "SE len: %s" % len(ses)
#print "SE mean: %s "% ses.mean()
#print "XS variance: %s "% xs.std()
return xs, ses
In [8]:
get_folder_info("\\\\DAP-NAS\\work\\CNOP\\dumps\\temp\\20150412_132614\\")
In [9]:
get_folder_info("\\\\DAP-NAS\\work\\CNOP\\dumps\\temp\\20150412_141301\\")
In [12]:
get_folder_info("\\\\DAP-NAS\\work\\CNOP\\dumps\\temp\\20150412_143656\\")
In [13]:
res03full = pickle_folder("\\\\DAP-NAS\\work\\CNOP\\dumps\\temp\\20150412_143656\\")
In [15]:
pickle.dump(res03full, file("\\\\DAP-NAS\\work\\CNOP\\dumps\\MC 16.04 -autocorrelation results\\resFull03", "w"))
In [56]:
xs, ses = process_dump(res03full, res_real_partial, cutpoints=(0.001,0.9))
In [57]:
del xs, ses, res03full
In [58]:
get_folder_info("\\\\DAP-NAS\\work\\CNOP\\dumps\\temp\\20150413_005610\\")
In [14]:
res06full = pickle_folder("\\\\DAP-NAS\\work\\CNOP\\dumps\\temp\\20150413_005610\\", chunksize=30)
In [15]:
pickle.dump(res06full, file("\\\\DAP-NAS\\work\\CNOP\\dumps\\MC 16.04 -autocorrelation results\\resFull06", "w"))
In [12]:
process_dump(res06full, res_real_partial, cutpoints=(0.001,0.9));
In [16]:
del res06full
In [17]:
res09full = pickle_folder("\\\\DAP-NAS\\work\\CNOP\\dumps\\temp\\20150413_112135\\", chunksize=30)
In [18]:
pickle.dump(res09full, file("\\\\DAP-NAS\\work\\CNOP\\dumps\\MC 16.04 -autocorrelation results\\resFull09", "w"))
In [19]:
process_dump(res09full, res_real_partial, cutpoints=(0.001,0.9));
In [20]:
del res09full
In [10]:
res09xs = pickle_folder("\\\\DAP-NAS\\work\\CNOP\\dumps\\temp\\20150415_000032\\", chunksize=30)
In [11]:
pickle.dump(res09xs, file("\\\\DAP-NAS\\work\\CNOP\\dumps\\MC 16.04 -autocorrelation results\\resXs09", "w"))
In [12]:
process_dump(res09xs, res_real_partial, cutpoints=(0.001,0.9));
In [13]:
del res09xs
In [14]:
res06xs = pickle_folder("\\\\DAP-NAS\\work\\CNOP\\dumps\\temp\\20150414_085358\\", chunksize=30)
In [15]:
pickle.dump(res06xs, file("\\\\DAP-NAS\\work\\CNOP\\dumps\\MC 16.04 -autocorrelation results\\resXs06", "w"))
In [16]:
process_dump(res06xs, res_real_partial, cutpoints=(0.001,0.9));
In [17]:
del res06xs
In [18]:
res03xs = pickle_folder("\\\\DAP-NAS\\work\\CNOP\\dumps\\temp\\20150413_234307\\", chunksize=30)
In [19]:
pickle.dump(res03xs, file("\\\\DAP-NAS\\work\\CNOP\\dumps\\MC 16.04 -autocorrelation results\\resXs03", "w"))
In [20]:
process_dump(res03xs, res_real_partial, cutpoints=(0.001,0.9));
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [35]:
type(res03full[45])
Out[35]: